<template>
	<view class="all-main">
		<view class="main">
			<view v-if="type != 3" class="type-item">
				<view class="content">
					<text v-html="content"></text>
				</view>
				<view class="msg">
					<text v-html="msg"></text>
				</view>
			</view>
			<view v-else class="type-item">
				<view class="content">
					<view class="title">
						<text>{{title}}</text>
					</view>
					<view class="lists">
						<view v-for="(item, index) in lists" :key="index" class="items">
							<view class="image">
								<u--image :src="API_URL + item.images" width="165" height="auto" mode="widthFix"></u--image>
							</view>
							<view class="number">
								<text>{{item.number}}</text>
							</view>
							<view class="titles">
								<text>{{item.title}}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view v-if="type != 3" class="occupy_position"></view>
			<view v-if="type != 3" class="bottom-btn">
				<view class="subBtn" @click="saveData">
					<text>保存模版</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	var util = require('@/util/request.js');
	export default {
		data() {
			return {
				type: 1,
				title: '',
				lists: [],
				content: '',
				msg: '',
				file_path: '',
				nav_title: '单位证明函模版',
				API_URL: '',
			}
		},
		onLoad(e) {
			this.API_URL = util.API_URL
			this.type = e.type
			if (this.type == 1) {
				this.nav_title = '单位证明函模版'
			} else if (this.type == 2) {
				this.nav_title = '授权书模版'
			} else if (this.type == 3) {
				this.nav_title = '授权书说明'
			}
			uni.setNavigationBarTitle({
				title: this.nav_title
			})
			this.loadData()
		},
		methods: {
			// 加载数据
			async loadData() {
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp({
					url: 'v1/getFileTemplate',
					data: {
						type: this.type
					}
				});
				uni.hideLoading()
				if (res.code == 200) {
					if (this.type == 3) {
						this.title = res.data.title
						this.lists = res.data.lists
					} else {
						this.content = res.data.content
						this.msg = res.data.msg
						this.file_path = res.data.file_path
					}
				}
			},
			// 下载文件
			saveData() {
				var that = this
				let url = encodeURI(that.file_path)
				uni.request({
					url: url, // 文件的下载链接
					success: (res) => {
						// 下载成功后将文件保存到本地
						uni.saveFile({
							tempFilePath: res.tempFilePath, // 下载的临时文件路径
							success: (res) => {
								console.log('保存成功', res.savedFilePath)
							},
							fail: (err) => {
								console.log('保存失败', err)
							}
						})
					},
					fail: (err) => {
						console.log('下载失败', err)
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.all-main {
		.main {
			padding: 20rpx;

			.type-item {
				.content {
					background-color: #fff;
					padding: 30rpx;
					border-radius: 16rpx;
					word-wrap: break-word;

					.title {
						text-align: center;
						margin-bottom: 30rpx;
						padding: 30rpx 0;
						font-weight: bold;
						font-size: 34rpx;
					}

					.lists {
						display: flex;
						align-items: center;
						flex-wrap: wrap;
						
						.items {
							width: 50%;
							text-align: center;
							font-size: 26rpx;
							
							.image {
								display: flex;
								justify-content: center;
							}
							
							.titles {
								height: 120rpx;
								padding: 0 40rpx;
							}
						}
					}
				}

				.msg {
					display: flex;
					justify-content: center;
					text-align: center;
					padding: 30rpx 60rpx;
					font-size: 24rpx;
					color: #909399;
				}
			}

			.occupy_position {
				height: 110rpx;
			}

			.bottom-btn {
				position: fixed;
				left: 0;
				right: 0;
				bottom: 0;
				padding: 30rpx;
				background-color: #ffffff;
				display: flex;
				justify-content: space-between;

				.subBtn {
					width: 100%;
					height: 80rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					background-color: #f2873a;
					color: #fff;
					border-radius: 14rpx;
				}
			}
		}
	}
</style>